#!/usr/bin/env python3

import gt
import os
import vt
import random

#  全局变量的定义
#  画布大小
ROW, COL = 20, 80
#  暂停标志位
pause = False
#  画布创建
convas = None
#  球的个数
ballNum = 10
#  球的属性描述
x, y, x_inc, y_inc, r, color, ch = None, None, None, None, None, None, None

def createConvas(r, c):
    """
    画布创建
    """
    convas = list(range(r))
    for x in convas:
        convas[x] = list(range(c))
    return convas

def initConvas(convas, row, col):
    """
    画布初始化
    """
    for r in range(row):
        for c in range(col):
            if r == 0 or c == 0 or r == row - 1 or c == col - 1:
                convas[r][c] = '#'
            else:
                convas[r][c] = ' '

    return convas

def showConvas(convas, row, col, color, ch, num):
    """
    刷新画布
    """
    vt.goto(back=False)
    for r in range(row):
        for c in range(col):
            if convas[r][c] in ch:
                vt.color(' ', vt.DEFAULT, color[ch.index(convas[r][c])],
                            back=False)
            else:
                print(convas[r][c], end='')
        print()

def draw(convas, row, col, x, y, r, ch):
    """
    在画布上作画 画一个球
    """
    for i in range(y, y + r):
        for j in range(x, x + r):
            if i > ROW - 1 or j > COL - 1 or i < 0 or j < 0:
                continue
            convas[i][j] = ch

def draws(convas, row, col, x, y, r, ch, num):
    """
    画所有球
    """
    for i in range(num):
        draw(convas, row, col, x[i], y[i], r[i], ch[i])

def createBall():
    """
    创建弹球
    """
    global x, y, x_inc, y_inc, r, color, ch

    x = list(range(ballNum))
    y = list(range(ballNum))
    x_inc = list(range(ballNum))
    y_inc = list(range(ballNum))
    r = list(range(ballNum))
    color = list(range(ballNum))
    ch = list(range(ballNum))

    for i in range(ballNum):
        r[i] = random.randint(1, 5)
        x[i] = random.randint(1, COL - r[i] - 1)
        y[i] = random.randint(1, ROW - r[i] - 1)
        x_inc[i] = 1
        y_inc[i] = 1
        color[i] = vt.getColor()
        ch[i] = '@*&%+-SA'[random.randint(0, 7)]


def ball_init(row, col):
    """
    弹球程序的初始化操作
    """
    vt.run([vt.SCREEN, vt.HIDE])
    gt.init()
    createBall()
    return createConvas(row, col)

def ball_exit(row):
    """
    弹球程序退出终端还原操作
    """
    vt.run([vt.goto(row + 2), vt.SHOW])
    gt.exit()
    os._exit(0)

def move(index):
    """
    一个弹球运动控制
    """
    global x, y, x_inc, y_inc, color

    if x[index] <= 0 or x[index] + r[index] > COL - 1:
        x_inc[index] *= -1
        color[index] = vt.getColor()

    if y[index] <= 0 or y[index] + r[index] > ROW - 1:
        y_inc[index] *= -1
        color[index] = vt.getColor()

    x[index] += x_inc[index]
    y[index] += y_inc[index]

def moves():
    """
    所有球的运动控制
    """
    for i in range(ballNum):
        move(i)

def ball():
    """
    弹球动画流程
    """
    if pause:
        return

    initConvas(convas, ROW, COL)
    draws(convas, ROW, COL, x, y, r, ch, ballNum)
    moves()
    showConvas(convas, ROW, COL, color, ch, ballNum)

    #  下一次动画显示的延时设置
    gt.run(ball, 0.1)

def keyHandle():
    global color, x_inc, y_inc, pause

    while True:
        ch = gt.get()
        if ch == 'q' or ord(ch) == 27:
            break
        elif ch == ' ':
            pause = not pause
            gt.run(ball)

if __name__ == '__main__':
    convas = ball_init(ROW, COL)
    gt.run(ball)
    keyHandle()
    ball_exit(ROW)


